package me.zk.work;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Hello world!
 *
 */
public class RabbitMqWorkConsumer0 {
    public static void main( String[] args ) throws IOException, TimeoutException {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        // 获取connection
        Connection connection = factory.newConnection();
        // 获取channel
        Channel channel = connection.createChannel();
        //使用对默认交换机
        channel.queueDeclare("work",true,false,false,null);
        channel.basicQos(1);

        //
        channel.basicConsume("work", false, new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("消息内容："+new String(delivery.getBody()));
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);

            }
        }, new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {
                System.out.println("CancelCallback");
            }
        });
//        channel.close();
//        connection.close();
    }
}
